This post is based on an R Markdown file published directly to WordPress.com - and you can do it, too! It was made possible by the great work of others, shared in the following highly recommended resources:
So nothing new here, other than recap of the above and confirmation that although the code behind this was produced in 2012-13, it still works as of August, 2021. :)
RMarkdown file available in this Github repo:
Summary of steps to be taken to publish R Markdown on WordPress.
The key to the process is the **RWordPress* package, supported by the XMLRPC package, both of which you can get using the code below:
As others have warned, the RWordPress package is no longer being maintained - so use at your own risk.
The other key is the ‘knit2wp’ function in the knitr package, hence the need to install that if not already installed, as well.
Example using my personal collection of Vancouver weather data ;), taken from Canadian government sources, available to all.
Idea here is to incorporate some Markdown common features that you may want to have in your blog post:
To explore the data across the period covered, summarize by year.
(Show some code)
## summarize annual temperature
annual_summary <- dataset %>% filter(Year<max(dataset$Year)) %>% ## remove most recent yr, since incomplete data
group_by(Year) %>% summarize(max_temp=max(Max.Temp, na.rm=TRUE),
min_temp=min(Min.Temp, na.rm=TRUE),
mean_daily_temp=mean(Mean.Temp, na.rm=TRUE),
annual_precip=sum(Total.Precip, na.rm=TRUE))
Get a glimpse of the structure:
## Rows: 51
## Columns: 5
## $ Year <dbl> 1970, 1971, 1972, 1973, 1974, 1975, 1976, 1977, 1978, …
## $ max_temp <dbl> 30.6, 29.4, 27.2, 28.9, 27.2, 27.8, 25.0, 30.5, 30.1, …
## $ min_temp <dbl> -7.2, -12.8, -11.1, -10.6, -8.3, -7.2, -6.7, -8.1, -13…
## $ mean_daily_temp <dbl> 9.43, 9.09, 9.07, 9.59, 9.91, 9.15, 9.70, 9.79, 9.96, …
## $ annual_precip <dbl> 875, 1335, 1244, 1000, 1248, 1320, 1008, 1032, 1017, 9…
Summary of the data:
## Year max_temp min_temp mean_daily_temp annual_precip
## Min. :1970 Min. :25.0 Min. :-15.20 Min. : 8.92 Min. : 810
## 1st Qu.:1982 1st Qu.:27.9 1st Qu.:-11.25 1st Qu.: 9.88 1st Qu.:1029
## Median :1995 Median :28.8 Median : -9.10 Median :10.27 Median :1207
## Mean :1995 Mean :28.8 Mean : -9.42 Mean :10.30 Mean :1174
## 3rd Qu.:2008 3rd Qu.:29.6 3rd Qu.: -7.70 3rd Qu.:10.80 3rd Qu.:1296
## Max. :2020 Max. :34.4 Max. : -3.10 Max. :11.45 Max. :1522
(Basic data viz - line chart using ggplot2)
I’m no climatologist, and this is very imprecise data exploration, but appears like there might be a gradual upward drift in temperatures over this relatively short period.
Vancouver is famous for it’s rainfall. How much rain has Vancouver received each year over the period covered? (1970 to 2020)
(years based on dynamic reference to data source)
That’s right…Vancouver frequently gets over 1,000 mm - 1 meter - of rain in a year!
Surely the rain is not steady all year long? What are the monthly patterns?
(Another visualization, this time with plotly (via ggplotly() function) for more interactivity.)
Dry in summer, wet in winter - especially November, December and, to lesser extent, January. By May, we’re usually safely out of the rainy season. Until October rolls around and the rains set in again.
Once your R Markdown file is polished to your liking, you just need to run some simple code to send it to WordPress:
1. load libraries
library(RWordPress)
library(knitr)
2. set credentials
options(WordpressLogin= c(<username>='<pwd>'),
WordpressURL=paste0('https://<blog name>.wordpress.com/xmlrpc.php'))
3. knit
knit2wp("<markdown file name>", ## markdown file to publish
title = "<post title>", ## title for the post in WordPress
publish = FALSE, ## FALSE to add as draft; TRUE to go direct to publish
action = "newPost", ## for new post; alternatives: "editPost", "newPage"
#postid ## needed with editPost - get from WP interface
shortcode= TRUE, ## optional
categories= c('R Markdown')) ## set categories, if desired
So you can set up a standard file and substitute values for markdown file and title as needed. Of course, you want to keep it private since you have password in there - add to .gitignore if you have a public Github repo.